//
//  MainNavigationController.swift
//  Here_Swift
//
//  Created by 童煊 on 2017/12/25.
//  Copyright © 2017年 童煊. All rights reserved.
//

import UIKit

class MainNavigationController: UINavigationController {

    override func viewDidLoad() {
        super.viewDidLoad()
        self.interactivePopGestureRecognizer?.delegate = nil
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        if self.childViewControllers.count > 0 {
            let button = UIButton.init(type: UIButtonType.custom)
            button.adjustsImageWhenHighlighted = false
            button.adjustsImageWhenDisabled = false
            button.setImage(#imageLiteral(resourceName: "icon_back"), for: UIControlState.normal)
            button.frame.size = CGSize.init(width: 70, height: 30)
            button.contentHorizontalAlignment = UIControlContentHorizontalAlignment.left
            button.setTitleColor(UIColorFromRGB(rgbValue: 0xfdfafa), for: UIControlState.normal)
            button.addTarget(self, action: #selector(back), for: UIControlEvents.touchUpInside)
            
            // 修改导航栏左边的item
            viewController.navigationItem.leftBarButtonItem = UIBarButtonItem.init(customView: button);
            
            // 隐藏tabbar
            viewController.hidesBottomBarWhenPushed = true;
        }
        
        // 这句super的push要放在后面, 让viewController可以覆盖上面设置的leftBarButtonItem
        super.pushViewController(viewController, animated: true)
    }
    
    @objc func back() -> Void {
        if ((self.presentedViewController != nil) || self.presentingViewController != nil) && self.childViewControllers.count == 1{
            self.dismiss(animated: true, completion: nil)
        }else{
            self.popViewController(animated: true)
        }
    }
}

extension MainNavigationController{
    public static func loadOnce(){
        let navBar = UINavigationBar.appearance()
        navBar.barTintColor = UIColor.white
        
        let attrs = NSMutableDictionary.init()
        attrs[NSAttributedStringKey.font] = UIFont.systemFont(ofSize: 16)
        attrs[NSAttributedStringKey.foregroundColor] = UIColorFromRGB(rgbValue: 0x3d3d3d)
        navBar.titleTextAttributes = attrs as? [NSAttributedStringKey : Any]
        
        if #available(iOS 9, *) {
            let barBtnItem = UIBarButtonItem.appearance(whenContainedInInstancesOf: [self])
            // UIControlStateNormal
            let itemAttrs = NSMutableDictionary.init()
            itemAttrs[NSAttributedStringKey.font] = UIFont.systemFont(ofSize: 16)
            itemAttrs[NSAttributedStringKey.foregroundColor] = UIColorFromRGB(rgbValue: 0x3d3d3d)
            barBtnItem.setTitleTextAttributes(itemAttrs as? [NSAttributedStringKey : Any], for: UIControlState.normal)
            // UIControlStateDisabled
            let itemDisableAttrs = NSMutableDictionary.init()
            itemDisableAttrs[NSAttributedStringKey.font] = UIFont.systemFont(ofSize: 16)
            itemDisableAttrs[NSAttributedStringKey.foregroundColor] = UIColorFromRGB(rgbValue: 0x3d3d3d)
            barBtnItem.setTitleTextAttributes(itemDisableAttrs as? [NSAttributedStringKey : Any], for: UIControlState.disabled)

        } else {
            
        }
    }
}
